package pers.test.votemanage.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.test.votemanage.dao.QuestionMapper;
import pers.test.votemanage.domain.Question;
import pers.test.votemanage.domain.dto.QuestionDTO;
import pers.test.votemanage.service.QuestionService;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public void insertQuestion(QuestionDTO questionDTO) {
        //service中传进来的DTO，需要拷贝到DO里面
        Question question1 = Question.builder().id(questionDTO.getId())
                .content(questionDTO.getContent()).build();
        //然后才将DO保存到数据库
        questionMapper.insertQuestion(question1);
    }

    @Override
    public QuestionDTO queryQuestion(String id) {
        //从数据库查出来的是DO
        Question question = questionMapper.queryQuestion(id);
        //要拷贝到DTO传输到controller，在controller进一步处理成VO返回给前端，DO是不会出现在Controller中的
        QuestionDTO questionDTO = new QuestionDTO();
        BeanUtils.copyProperties(question,questionDTO);
        return questionDTO;
    }
}
